package model

import (
	"github.com/kataras/iris"
	"github.com/go-xorm/xorm"
	_ "github.com/go-sql-driver/mysql"
	"errors"
	"fmt"
	"github.com/ngaut/log"
)

var MainDB *xorm.Engine

// 初始化数据库信息
func InitMainDB(dsn string) error {
	var err error
	MainDB, err = xorm.NewEngine("mysql", dsn)
	if err != nil {
		return errors.New(fmt.Sprintf("database init error, msg: %s", err.Error()))
	}

	iris.RegisterOnInterrupt(func() {
		log.Info("database connection closed normally")
		MainDB.Close()
	})
	MainDB.SetMaxIdleConns(0)
	MainDB.SetMaxOpenConns(50)
	MainDB.SetConnMaxLifetime(0)
	return nil
}

